<%@ page import="java.sql.*" %>
<%@ page import="com.shop.util.JsUtils" %>

<%--
  Created by IntelliJ IDEA.
  User: 刘员君
  Date: 2024/9/3
  Time: 10:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    /*
    一、接收数据；
    二、验证数据；
    三、连接数据库，插入数据，实现注册；
     */
    //设置字符集中文编码
    request.setCharacterEncoding("utf8");
    //接收数据
    String uname=request.getParameter("uname");
    String upwd=request.getParameter("upwd");
    String reupwd=request.getParameter("reupwd");
    String uemail=request.getParameter("uemail");
    String uselect=request.getParameter("uselect");
    //验证数据
    if (uname.isEmpty()){
        JsUtils.JsAlertBack(out,"用户名不能为空！");
        return;
    }
    if (upwd.isEmpty()){
        JsUtils.JsAlertBack(out,"密码不能为空！");
        return;
    }
    if(upwd.length()<6){
        JsUtils.JsAlertBack(out,"密码长度至少为6！");
        return;
    }
    if (!upwd.equals(reupwd)){
        JsUtils.JsAlertBack(out,"两次密码不一致！");
        return;
    }
    if (uemail.isEmpty()){
        JsUtils.JsAlertBack(out,"邮箱不能为空！");
        return;
    }
    //正则表达式：
    String email="[a-zA-Z0-9]+@[a-zA-Z0-9].+[a-zA-Z]{2,3}";
    if (!uemail.matches(email)){
        JsUtils.JsAlertBack(out,"邮箱格式不对！");
        return;
    }
    if (uselect==null){
        JsUtils.JsAlertBack(out,"请勾选同意协议！");
        return;
    }

    //连接数据库，插入数据，实现注册
    //加载mysql驱动类
    Class.forName("com.mysql.jdbc.Driver");
    String url="jdbc:mysql://localhost:3306/onlineshop?useUnicode=true&characterEncoding=utf8";
    String dbUser="root";
    String dbPwd="root";
    //创建数据库连接对象
    Connection conn= DriverManager.getConnection(url,dbUser,dbPwd);
    //创建执行SQL语句的对象
    Statement stmt=conn.createStatement();
    //编写SQL语句并执行
    String sqlInsert="insert into users(uname,upwd,uemail,udate) values('"+uname+"','"+upwd+"','"+uemail+"',now())";
    int num=stmt.executeUpdate(sqlInsert);
    if (num>0){
        JsUtils.JsAlertGo(out,"注册成功！","login.jsp");
    }else{
        JsUtils.JsAlertBack(out,"注册信息有误，注册失误！");
    }
%>
</body>
</html>

